package core;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import basic.QuerySet;
import common.Service;
import common.Servlet;
import common.Validate;
import dict.CbDict;
import dict.CommonDict;
import dict.TransactionDict;
import dicts.GlobalDict;
import dicts.PaymentDict;
import entities.TTransaction;
import interfaces.IMsService;
import interfaces.ITransactionService;
import net.sf.json.JSONObject;
import util.LogUtil;
import utils.DateUtil;
import utils.NumberUtil;
import utils.StringUtil;

public class Thaw extends Servlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public synchronized void process(HttpServletRequest request, HttpServletResponse response) throws Exception {

		System.out.println(DateUtil.format(new Date(), DateUtil.DATETIME) + " | 冻结撤销");

		// 初始化service
		ITransactionService transactionService = (ITransactionService) Service.create(ITransactionService.class);
		IMsService msService = (IMsService) Service.create(IMsService.class);

		QuerySet<Object> querySet = new QuerySet<Object>();
		Map<String, Object> paramMap = Collections.synchronizedMap(new HashMap<String, Object>());
		JSONObject jsonResult = new JSONObject();

		// 初始化bean和属性
		TTransaction transaction = new TTransaction();

		// 验证系统
		if (GlobalDict.LINUX.equalsIgnoreCase(System.getProperty("os.name")) && !CommonDict.HOST.equals(request.getServerName())) {
			response.getWriter().print(Validate.result(TransactionDict.ILLAGEL_ACCESS, TransactionDict.MESSAGES.get(TransactionDict.ILLAGEL_ACCESS)));
			return;
		}

		// 校验参数 ( 名称,值,校验模式,边界 )
		List<Object[]> parameterList = Collections.synchronizedList(new ArrayList<Object[]>());
		parameterList.add(new Object[] { "txnNo", request.getParameter("txnNo"), Validate.NOTEMPTY | Validate.EXCEEDLENGTH, 0, 50 });

		JSONObject validateResult = Validate.validate(parameterList);
		LogUtil.thaw(Validate.summary(parameterList));

		if (!StringUtil.isEmpty(validateResult)) {
			System.out.println ( validateResult.toString() ) ;
			response.getWriter().print(validateResult.toString());
			return;
		}
		
		// 查出对应交易
		try {
			transaction = transactionService.fetch(request.getParameter("txnNo"));
		} catch (Exception e) {
			response.getWriter().print(Validate.result(TransactionDict.TRANSACTION_FETCH_EXCEPTION, TransactionDict.MESSAGES.get(TransactionDict.TRANSACTION_NOT_EXIST)));
			return;
		}
		if (transaction == null) {
			response.getWriter().print(Validate.result(TransactionDict.TRANSACTION_NOT_EXIST, TransactionDict.MESSAGES.get(TransactionDict.TRANSACTION_NOT_EXIST)));
			return;
		}
		if (!transaction.getTxnState().equals("1")) {
			response.getWriter().print(Validate.result(TransactionDict.TRANSACTION_STATUS_EXCEPTION, TransactionDict.MESSAGES.get(TransactionDict.TRANSACTION_STATUS_EXCEPTION)));
			return;
		}
		
		// 交易是否有冻结记录存在
		try {
			paramMap = Collections.synchronizedMap(new HashMap<String, Object>());
			paramMap.put("txnNo", transaction.getTxnNo());
			paramMap.put("msType", PaymentDict.MS_FREEZE);
		} catch (Exception e) {
			response.getWriter().print(Validate.result(CbDict.CB_FETCH_EXCEPTION, CbDict.MESSAGES.get(CbDict.CB_FETCH_EXCEPTION)));
			return;
		}

		if (!msService.contain(paramMap)) {
			response.getWriter().print(Validate.result(TransactionDict.MS_NOT_EXIST, TransactionDict.MESSAGES.get(TransactionDict.MS_NOT_EXIST)));
			return;
		}
		
		//删除冻结
		try {
			paramMap = Collections.synchronizedMap(new HashMap<String, Object>());
			paramMap.put("txnNo", transaction.getTxnNo());
			paramMap.put("msType", PaymentDict.MS_FREEZE);
			querySet.setParamMap(paramMap);
			querySet.setPolymerizeInfo("msAmount");
			transaction.setTxnRemain(NumberUtil.add(transaction.getTxnRemain(), msService.summary(querySet)));
			transaction.setTxnRefuse("0");
			transactionService.persist(transaction);
			
			paramMap.put("txnNo", transaction.getTxnNo());
			paramMap.put("msType", PaymentDict.MS_FREEZE);
			msService.expunge(paramMap);
		} catch (Exception e) {
			response.getWriter().print(Validate.result(CbDict.CB_REMOVE_EXCEPTION, CbDict.MESSAGES.get(CbDict.CB_REMOVE_EXCEPTION)));
			return;
		}

		jsonResult.put("code", "0000");
		jsonResult.put("message", "Revoke success");

		response.getWriter().print(jsonResult.toString());
		return;
	}
}
